package com.skyline.manage.feed.task;


import org.apache.log4j.Logger;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.skyline.manage.feed.ConfigLoader;

/**
 * 
 * @author jairus
 * 
 */
public class FeedDeleteTaskElement {
	
	private Logger logger = Logger.getLogger(FeedDeleteTaskElement.class);

	private DBCollection DELETE_FROM_COLLECTION;

	private Integer LIFE;

	private String TIME_COLUMN;
	
	private String COLLECTION_NAME;

	public FeedDeleteTaskElement(DBCollection deleteFromCollection,Integer life) {
		this.DELETE_FROM_COLLECTION = deleteFromCollection;
		this.LIFE = life;
		this.COLLECTION_NAME=deleteFromCollection.getFullName();
		this.TIME_COLUMN = ConfigLoader.getValue("feed.column.time");
	}

	public void run() {
		Long time = System.currentTimeMillis() - this.LIFE;
		DBObject query = new BasicDBObject();
		DBObject timeRequiration = new BasicDBObject("$lt", time);
		query.put(TIME_COLUMN, timeRequiration);
		DBCursor cursor = DELETE_FROM_COLLECTION.find(query);
		int total=0;
		while (cursor.hasNext()) {
			DELETE_FROM_COLLECTION.remove(cursor.next());
			total++;
		}
		logger.info("在"+this.COLLECTION_NAME+"共删除"+total+"条记录");
	}
}
